//工具
let tool = {
    on: function (element, type, func) {
        if (element.addEventListener) {
            element.addEventListener(type, func, false); //false 表示冒泡
        } else if (element.attachEvent) {
            element.attachEvent('on' + type, func);
        } else {
            element['on' + type] = func;
        }
    },
    getPageHeight: function (element) {
        return element.scrollHeight;
    },
    // 获取页面卷去的高度
    getScrollTop: function (element) {
        return element.scrollTop;
    },
    // 获取页面可视区域宽度
    getClientHeigth: function (element) {
        return element.clientHeight;
    },
    timer: null,
    timer2: null,
    timer3: null
};

//基础的向后台请求数据，然后对屏幕进行自适应
let MainToPage = {
    _data: null,
    _url: null,
    _method: null,
    _type: "Json",
    _func: null,
    _shareArray: [],
    _adjustWindowMethod: null,
    _failResult: null,
    setBase: function (data, url, method, type, func) {
        this._data = data;
        this._url = url;
        this._method = method;
        if (type) {
            this._type = type;
        }
        if (func) {
            this._func = func;
        }
    },
    //设置总节点信息
    setNodeListToShow: function (nodeListToShow, adjustWindowMethod, failResult) {
        this._shareArray = nodeListToShow;
        this._adjustWindowMethod = adjustWindowMethod;
        if (failResult) {
            this._failResult = failResult;
        }
        else {
            this._failResult = function () {
            };
        }
    },
    //设置第一次请求
    setFirstRequest: function (flag) {
        let that = this;
        ajax_base(this._data, this._url, this._method, this._type, this._func).then(function (data) {
            if (data && data.obj.length === 0) {
                that._failResult();
                return;
            }
            let length = that._adjustWindowMethod(that._shareArray.getPlayNode());
            that._shareArray.showAllNode(data.obj, length, that._data.pageNum, flag);
        });
    },

    setWindowResize: function (nodeName) {
        let that = this;
        tool.on(window, 'resize', function () {
            clearTimeout(tool.timer);
            tool.timer = setTimeout(function () {
                let arrayNodeList = screenAdopt.getAllNextChildNode(nodeName);
                screenAdopt.removeChildrenNodes(nodeName, arrayNodeList.arrayAllChildrenNode);
                if (arrayNodeList.arrayAllChildrenNode[0].className && arrayNodeList.arrayAllChildrenNode[0].className === "searchResult-null-text") {
                    that._failResult();
                    return;
                }
                let length = that._adjustWindowMethod(that._shareArray.getPlayNode());
                that._shareArray.adjustScreen(arrayNodeList, length);
            }, 50)
        });
    },
    setRollWithWindow: function (nodeName1, nodeName2, lengthOfBottom, flag) {//nodeName1为父节点（一般是含有inner的），nodeName2为包含显示块的节点
        let scroll = document.querySelector("." + nodeName1),
            lengthToAscend = 0,
            that = this;
        if (lengthOfBottom) {
            lengthToAscend = lengthOfBottom;
        }
        tool.on(scroll, 'scroll', function () {
            clearTimeout(tool.timer2);
            if (screenAdopt.getMinHeight(nodeName2) + lengthToAscend < tool.getScrollTop(scroll)) {
                tool.timer2 = setTimeout(function () { //缓存
                    that._data.pageNum++;   //分页
                    ajax_base(that._data, that._url, that._method, that._type, that._func).then(function (data) {
                        if (data && data.obj.length === 0) {
                            return;
                        }
                        if (data.obj && data.obj.length >= 1) {
                            let length = that._adjustWindowMethod(that._shareArray.getPlayNode());
                            that._shareArray.showAllNode(data.obj, length, that._data.pageNum, flag);
                        }
                    });
                }, 50)
            }
        });
    }
};
/*左边栏显示效果的实例*/
let FadeModelShowToWindow = {
    _fadeM: new fadeModel(),
    init: function (resize) {
        this._fadeM.checkFade();
        this._fadeM.startMenuShow();
        if (resize === "resize") {
            let that = this;
            tool.on(window, 'resize', function () { //监视窗口动作
                that._fadeM.changeRightPadding();
            })
        }
    }
};